package com.atguigu.gulimall.search.thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 异步编排测试-线程串行化
 *
 * @author LaiSanShan
 * @date 2021-08-16
 */
public class CompletableFuture2Test {
    private static ExecutorService threadPool = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main。。。开始" + Thread.currentThread().getId());
        // 1.
//        CompletableFuture.supplyAsync(() -> {
//            System.out.println("第一个任务执行，返回10");
//            return 10;
//        }, threadPool)
//                .thenRunAsync(() -> {
//                    System.out.println("第二个任务执行，不返回");
//                }, threadPool);
        // 2.
//        CompletableFuture.supplyAsync(() -> {
//            System.out.println("第一个任务执行，返回10");
//            return 10;
//        }, threadPool)
//                .thenAcceptAsync((res) -> {
//                    System.out.println("拿到第一个任务执行的结果：" + res);
//                    System.out.println("第二个任务不返回");
//                }, threadPool);
        // 3.
        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("第一个任务执行，返回10");
            return 10;
        }, threadPool)
                .thenApplyAsync((res) -> {
                    System.out.println("拿到第一个任务执行的结果：" + res);
                    System.out.println("第二个任务返回11");
                    return 11;
                }, threadPool);
        Integer res = completableFuture.get();
        System.out.println("拿到最终结果：" + res);
        System.out.println("main。。。结束");
    }

}
